@ECHO OFF
TITLE TCC
:CMDMAIN
IF /I "%1" == ""            (CALL:DEFAULT)
IF /I "%1" == "SET"         (CALL:CCSET)
IF /I "%1" == "CPFF"        (CALL:CPFFMPEG)
IF /I "%1" == "BUILD"       (CALL:BUILD EXE)
IF /I "%1" == "BUILDDLL"    (CALL:BUILD DLL)
IF /I "%1" == "BUILD86"     (CALL:ONLYBUILDX86)
IF /I "%1" == "RUN"         (CALL:RUN)
GOTO EOF
:DEFAULT
IF NOT EXIST "%~dp0TCC.exe" MKLINK %~dp0TCC.exe .\LIB\TCCx64.exe&CLS
SET PATH=%PATH%;%~DP0;%~DP0\LIB;
CD /D %~DP0&%COMSPEC% /K 
GOTO EOF
:CCSET
SET MLIB=%CCPATH%LIB
SET PATH=%PATH%;%MLIB%
SET CCBIN=%MLIB%
SET CINC=%CCPATH%include;%CCPATH%include\winapi;%CCPATH%include\ddk
SET CLIB=%MLIB%
SET FFDEF=%CCBIN%\ffmpeg.def %CCBIN%\user32.def %CCBIN%\gdi32.def %CCBIN%\opengl32.def %CCBIN%\ws2_32.def %CCBIN%\winmm.def %CCBIN%\advapi32.def %CCBIN%\shell32.def %CCBIN%\ntdll.def
SET FFDEF64=%CCBIN%\ffmpeg64.def %CCBIN%\user32.def %CCBIN%\gdi32.def %CCBIN%\opengl32.def %CCBIN%\ws2_32.def %CCBIN%\winmm.def %CCBIN%\advapi32.def %CCBIN%\shell32.def %CCBIN%\ntdll.def
SET CC86=%MLIB%\TCCX86
SET CC64=%MLIB%\TCCX64
SET RUN=%MLIB%\TCCRUN
SET SIGN=%MLIB%\SIGNFLAG.EXE
SET RC86=%MLIB%\WINDRES
SET RC64=%MLIB%\WINDRES64
SET WORKNAME=%NAME%
SET RESRC=%WORKNAME%\%RES%.rc
SET RES86=%RES%.o
SET RES64=%RES%64.o
GOTO EOF
:CPFFMPEG
COPY /Y %CCPATH%LIB\FFMPEG*.SO %CBIN%
GOTO EOF
:ONLYBUILDX86
SET COUT=%CBIN%%NAME%.exe
%CC86% -I %CINC% -L %CLIB% -w %CSRC%%CNAME% %FFDEF% %FUNDEF% -o %COUT%
%SIGN% %COUT%
@DEL /S /Q *.o
GOTO EOF
:BUILD
REM BUILD EXE OR DLL
IF "%1"=="DLL" (SET EXTNAME=dll&SET STRDLL=-shared) ELSE (SET EXTNAME=exe&SET STRDLL=)
SET COUT=%CBIN%%NAME%.%EXTNAME%
SET COUT64=%CBIN%%NAME%64.%EXTNAME%
REM HAVE RES OR NOT
IF "%RES%"=="" (
%CC86% -I %CINC% -L %CLIB% -O3 -w %STRDLL% %CSRC%%CNAME% %FFDEF% %FUNDEF% -o %COUT%&%SIGN% %COUT%
%CC64% -I %CINC% -L %CLIB% -O3 -w %STRDLL% %CSRC%%CNAME% %FFDEF64% %FUNDEF64% -o %COUT64%&%SIGN% %COUT64%
) ELSE (
REM HAVE RES
::windres.exe -O coff myres.rc -o myres.o
%RC86% -o coff -I ./%WORKNAME% %RESRC% -o %RES86%
%RC64% -o coff -I ./%WORKNAME% %RESRC% -o %RES64%
%CC86% -I %CINC% -L %CLIB% -O3 -w %STRDLL% %CSRC%%CNAME% %RES86% %FFDEF% %FUNDEF% -o %COUT%
%SIGN% %COUT%
%CC64% -I %CINC% -L %CLIB% -O3 -w %STRDLL% %CSRC%%CNAME% %RES64% %FFDEF64% %FUNDEF64% -o %COUT64%
%SIGN% %COUT64%
@DEL /S /Q *.o
)
GOTO EOF
:RUN
REM ONLY RUN X86
::%RUN% -w %FFDEF% -run %CSRC%%CNAME%
%RUN% -I %CINC% -L %CLIB% -w %FFDEF% -run %CSRC%%CNAME%
::TIMEOUT 1
GOTO EOF
:EXAMPLE
::=================================================================
SET CCPATH=%~DP0..\..\
SET NAME=HELLO
SET CNAME=HELLO.C
::SET FUNDEF=XX.DEF
::SET FUNDEF64=XX64.DEF
SET RES=myres
SET CSRC=%~DP0\
SET CBIN=%~DP0\
::SET PATH
CALL %CCPATH%TINYC.CMD SET
::BUILD EXE
CALL %CCPATH%TINYC.CMD BUILD
::BUILD DLL
CALL %CCPATH%TINYC.CMD BUILDDLL
::NO BUILD GO RUN 
CALL %CCPATH%TINYC.CMD RUN
TIMEOUT 10
::=================================================================
GOTO EOF
:EOF
